<template>
	<view class="route-recharge">

		<template v-if="type==2">
			<view @click="handleClick(item)" v-for="(item,index) in blocksList" :key="index" class="one-package-gou">
				<view class="package-box">
					<image :src="item.image[0]" mode="aspectFill" class="package-img"></image>
					<view class="package-name-box">
						<view class="package-name">{{item.name}}</view>
						<view class="chongzhiContent">
							{{item.content}}
						</view>
						<view class="package-price">
							<text>¥ {{item.price.toFixed(2)}}</text>
							<view class="">
								<u-button color="#28E06E" size="small" type="primary" icon="shopping-cart"
									text="立即购买"></u-button>
							</view>
						</view>
					</view>
				</view>
			</view>
		</template>

		<template v-else>
			<view class="recharge_yue">
				<!-- <view class="input_money">
					<text>实充金额：</text>
					<u--input placeholder="请输入金额" type="number" border="none" v-model="value"
						@change="change"></u--input>
				</view> -->
				<view class="recharge_title">
					充值方案
				</view>
				<view class="" v-if="blocksList.length>0">
					<view @click="handleChangeActive(item)" v-for="(item,index) in blocksList" :key="index"
						class="one-package" :class="{'sel-pac':blockpackageid===item.id}">
						<view class="package-name">{{item.name}}
							<text v-if="item.discount>0">（享受折扣{{item.discount}}折）</text>
						</view>
						<view class="package-box">
							<view class="package-price">¥ {{item.price.toFixed(2)}}</view>
							<image v-if="blockpackageid==item.id" src="/static/images/checked.png" mode="widthFix"
								class="check-img">
							</image>
							<image v-else src="/static/images/check.png" mode="widthFix" class="check-img"></image>
						</view>
					</view>
					<u-gap height="80" bgColor="#fff"></u-gap>
					<view class="">
						<view class="recharge_title">
							实际到账
						</view>
						<view class="">
							<view class="">
								<text>实际到账金额：</text>
								<text style="font-size: 24rpx;color: #da3f2d">￥{{couponInfo.song+couponInfo.price}}</text>
							</view>
							<view class="" style="font-size: 24rpx;color: #939BA7">
								（含赠送金额：{{couponInfo.song}}元）
							</view>
						</view>
					</view>
				</view>

				<view class="" v-else>
					<u-empty mode="list" text="本店暂未设置充值套餐">
					</u-empty>
				</view>
				<view class="footer">
					<view class="btn" @click="handleRecharge">
						充值
					</view>
					<button open-type="share" class="btn" style="margin-top: 20rpx;" v-if="type===2">
						分享
					</button>
				</view>
			</view>

		</template>
	</view>
</template>

<script setup>
	import subScribeMsg from '@/utils/subScribeMsg.js'
	import handleCache from "@/utils/cache.js"
	import {
		balanceApi,
		userApi
	} from '@/api/index.js'
	import {
		ref,
		computed
	} from 'vue'
	import {
		onReady,
		onShow,
		onLoad
	} from '@dcloudio/uni-app'
	const userInfo = ref('')
	const blocksList = ref([])
	const blockpackageid = ref('')
	const type = ref(1)
	const shareShanghuId = ref('')
	const couponInfo = ref([])

	onShow(async () => {
		console.log('xxxxxxxxxxxxxxxx哈哈哈哈哈哈哈哈')
		userInfo.value = handleCache.get('userInfo')
		if (shareShanghuId.value) {
			if (!handleCache.get('userInfo')) {
				uni.navigateTo({
					url: `/pages/login/login?path=/pages/recharge/recharge?type=${type.value}&shanghuId=${shareShanghuId.value}&pid=${userInfo.value.id}`
				})
			} else {
				await userApi.userstore({
					uid: userInfo.value.id,
					shanghuid: shareShanghuId.value
				}).then((res) => {
					if (res.code == 0) {
						let data = {
							...userInfo.value
						}
						data.shanghuid = shareShanghuId.value
						userInfo.value = data
						handleCache.remove('userInfo')
						handleCache.set('userInfo', data, 'forever')
					}
				})
			}
		}
		await getData()
		if (type.value === 2) {
			uni.setNavigationBarTitle({
				title: '充值购'
			})
			uni.$mpShare = shareData.value
		}

	})
	onLoad((options) => {
		console.log('zheshi xxxxxxx', options)
		if (options.shanghuid) {
			shareShanghuId.value = Number(options.shanghuid)
		}
		if (options.type) {
			type.value = Number(options.type)
		}
		if (options.pid) {
			if (options.type == 2) {
				getApp().globalData.shareUserId = Number(options.pid)
				if (handleCache.get('userInfo')) {
					userApi.bangdingsj({
						uid: handleCache.get('userInfo').id,
						pid: Number(options.pid)
					})
				}
			}

		}

	})
	const shareData = computed(() => { // 分享的数据
		return {
			title: `${uni.getStorageSync('userInfo').value.shanghuname||''}-充值购`,
			path: `/pagesA/recharge/recharge?type=${type.value}&id=${userInfo.value.id}&shanghuid=${userInfo.value.shanghuid}`,
			desc: '快来看看',
		}
	})
	const getData = () => {
		balanceApi.blocks({
			shanghuid: userInfo.value.shanghuid,
			state: type.value
		}).then((res) => {

			for (var i = 0; i < res.data.length; i++) {
				console.log(res.data[i].image);
				if (res.data[i].image != '') {
					var links = res.data[i].image.split(",");
					res.data[i].image = links;
					res.data[i].content = unescape(res.data[i].content.replace(/\\/g, "%"));
				}
			}
			blocksList.value = res.data;
			if (blocksList.value.length) {
				blockpackageid.value = blocksList.value[0].id;
				couponInfo.value= blocksList.value[0];
			}
		})
	}
	const handleChangeActive = (data) => {
		blockpackageid.value = data.id;
		console.log('这是优惠券信息：', data)
		couponInfo.value = data;

	}

	// 充值购item点击事件 res.data.data[i].content = unescape(res.data.data[i].content.replace(/\\/g, "%"));
	const handleClick = (data) => {
		console.log('xxxx', data)
		uni.navigateTo({
			// url: "/pagesA/recharge_detail/recharge_detail?"
			url: `/pagesA/recharge_detail/recharge_detail?id=${data.id}&shanghuid=${data.shanghuid}&type=2`
		})
	}

	const handleRecharge = async () => {
		if (!blockpackageid.value) {
			return
		}
		if (type.value === 2) {
			await subScribeMsg(['Kj8bNW0GPT0RnaKCVKKT0_ryLsog6V2jrB2T-AwGWJw'])
		}
		let req = {
			uid: userInfo.value.id,
			blockpackageid: blockpackageid.value
		}
		uni.showLoading({
			text: '加载中'
		})
		balanceApi.wechatpay(req).then((res) => {
			uni.hideLoading()
			if (res.code === 0) {
				let paydata = res.data

				wx.requestPayment({
					nonceStr: paydata.nonceStr, // 随机字符串
					package: paydata.package, // 固定值
					signType: paydata.signType, // 统一下单订单号 
					timeStamp: paydata.timeStamp, // 时间戳（单位：秒）
					paySign: paydata.sign, // 签名，这里用的 MD5/RSA 签名
					success(res) {
						if (type.value === 2) {
							uni.navigateTo({
								url: "/pagesA/rechargeorder/rechargeorder"
							})
							return
						}
						uni.navigateTo({
							url: '/pages/payresult/payresult?status=1'
						})
					},
					fail(e) {
						uni.navigateTo({
							url: '/pages/payresult/payresult?status=0'
						})

					}
				})
			} else {
				uni.showToast({
					mask: true,
					title: res.msg,
					icon: 'none'
				})
			}
		})
	}
</script>

<style lang="scss" scoped>
	button {
		margin: 0;
		padding: 0;
		background-color: transparent;
		line-height: 32rpx;
	}

	button::after {
		border: none;
	}

	.route-recharge {
		padding: 30rpx 24rpx;

		.inner {
			background-color: #fff;
			border-radius: 10rpx;
			padding: 24rpx;

			.tit {
				font-weight: 500;
				font-size: 30rpx;
			}

			.cont {
				display: flex;
				flex-wrap: wrap;
				justify-content: space-between;

				.cloum {
					margin-top: 20rpx;
					width: calc(50% - 10rpx);
					height: 100rpx;
					border: 1px solid #edf0f2;
					border-radius: 12rpx;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					background-color: #fff;
				}

				.val {
					font-weight: 500;

				}

				.name {
					font-size: 24rpx;
					margin-top: 5rpx;
					color: #999;
				}

				.active {
					border-color: #EE382F;

					color: #EE382F;
				}
			}

		}

		.footer {
			margin-top: 70rpx;

			.btn {
				width: 100%;
				height: 80rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				background-color: #EE382F;
				color: #fff;
				border-radius: 50rpx;
			}
		}
	}

	.one-package {
		padding: 24rpx 24rpx;
		box-sizing: border-box;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		border-radius: 12rpx;
		background: #fff;
		width: 100%;
		margin-bottom: 20rpx;
		border: 2px solid #fff;
	}

	.sel-pac {
		background: rgba(#ffdada, 0.2);
		border-radius: 12px 12px 12px 12px;
		border: 2px solid rgba(#EE382F, 0.7);
	}

	.package-img {
		width: 200rpx;
		height: 200rpx;
		border-radius: 10rpx;
	}

	.package-name {
		font-weight: 700;
		font-size: 26rpx;
		margin-right: 20rpx;
	}

	.package-price {
		font-weight: 700;
		font-size: 36rpx;
		color: #F13658;
	}

	.check-img {
		margin-left: 40rpx;
		width: 40rpx;
		height: 40rpx;
	}

	.package-box {
		display: flex;
		justify-content: space-between;
	}

	.one-package-gou {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		background: #fff;
		padding: 20rpx 20rpx;
		box-sizing: border-box;
		border-radius: 20rpx;
	}

	.package-name-box {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		justify-content: space-between;
		// height: 150rpx;
		margin-left: 40rpx;
		width: 70%;
	}

	.chongzhiContent {
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
		text-overflow: ellipsis;
		margin: 10rpx 0 20rpx 0;

	}

	.recharge_yue {
		background-color: #fff;
		border-radius: 20rpx;
		padding: 20rpx;
	}

	.input_money {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.recharge_title {
		margin: 20rpx 0;
		font-weight: 700;
		font-size: 30rpx;
	}
</style>